﻿using System;
using System.Threading;
using System.Web.UI;
using Phidgets;

namespace demo
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLeft_Click(object sender, EventArgs e)
        {
            Stepper stepper = null;
            try
            {
                //Declare a Stepper object
                stepper = new Stepper();

                //Open the Stepper object for device connections
                stepper.open();

                //Wait for a MotorControl device to be attached
                stepper.waitForAttachment();

                //Set the max velocity to start the stepper motor moving until it hits 
                //the goal position
                stepper.steppers[0].VelocityLimit = stepper.steppers[0].VelocityMax;
                stepper.steppers[0].Acceleration = stepper.steppers[0].AccelerationMin; //ensure the value is between the AccelerationMin and AccelerationMax
                stepper.steppers[0].TargetPosition = 0;
                stepper.steppers[0].Engaged = true;
                //Set the goal position to 2000
                stepper.steppers[0].TargetPosition = -9000;
                Thread.Sleep(2 * 1000);
            }
            catch (PhidgetException ex)
            {
                Response.Write(ex.ToString());
            }
            finally
            {
                if (null != stepper)
                {
                    stepper.steppers[0].Engaged = false;
                    //close the Stepper object
                    stepper.close();

                    //set the object to null to get it out of memory
                    stepper = null;
                }
            }
        }

        protected void btnRight_Click(object sender, EventArgs e)
        {
            Stepper stepper = null;
            try
            {
                //Declare a Stepper object
                stepper = new Stepper();

                //Open the Stepper object for device connections
                stepper.open();

                //Wait for a MotorControl device to be attached
                stepper.waitForAttachment();

                //Set the max velocity to start the stepper motor moving until it hits 
                //the goal position
                stepper.steppers[0].VelocityLimit = stepper.steppers[0].VelocityMax;
                stepper.steppers[0].Acceleration = stepper.steppers[0].AccelerationMin; //ensure the value is between the AccelerationMin and AccelerationMax
                stepper.steppers[0].TargetPosition = 0;
                stepper.steppers[0].Engaged = true;
                //Set the goal position to 2000
                stepper.steppers[0].TargetPosition = 9000;
                Thread.Sleep(2 * 1000);
            }
            catch (PhidgetException ex)
            {
                Response.Write(ex.ToString());
            }
            finally
            {
                if (null != stepper)
                {
                    stepper.steppers[0].Engaged = false;
                    //close the Stepper object
                    stepper.close();

                    //set the object to null to get it out of memory
                    stepper = null;
                }
            }
        }
    }
}
